﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace TempConsole;

public class RSACG
{
    public static void SignDemo()
    {
        var privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDAJQ5aL9G5B9QgumyBNfcxvrtJ4ZdBCIoaIGGLSQnfDHXVAc6/SVcnpKkr6jgN/iaDt7c2YroNxCbaTZQCIG4pSPIPnRE99T3XAjYTiNg8SnlNhiOAKB/U2ZuBoWuzRtkU+sfd7MfHuxBta8UoKsEK/7yQJf2Q4tKiWORH30GXJIeOeecOLnoF5w1c8NOdeWDK59rIocPYNBKaAFeCbyr+lTIob8iiPnpEDgSdPWFKGCwGlyCUux/3H6rIA+6oAMK+SIRDZfgyosoYsEJ1Kgv/+MR7rCOo6PYi/594UF/YVhn9FV0/ohRn1usRF18ERsgJQorWuX4ibJAHjDsWbyJzAgMBAAECggEAYAnli5R9OgO8toMXutmzbYzM5he410uiY36t4sWmBnOe7dHSee/5fRojaIzQacYPkCSika91GW5ikRjXktyM01vfiYhTVumeeDpltwveMPx0dxhF5Eep6WS/AmMhhuExnfffMICqNp28j0Sfrfz4sjuzD0W94M5Ow9C0gWwIx7ceu3J110EJRwobTMGybHbc13t0Er7Btk2nhQvKJ6quBIDFU4Z4YI94BcCy0oynnkCAJk3yTvCRh0ytvs3uxHxqknybWwJZs4L4DFeZCz/MWZWlMh31rLBDjAmLLkrjCbite0UknmxNb9LUY47D3HTl1eb1X80fk/2Tc2zD/+70yQKBgQD0cPkVbzprjGptqXjQw6ADcKA2B0GRH/BvFbIaSrqLzV9nhJ5+er4Fh/ysd5ZF01oRs35e7FdBeYBdL8jiazuCgd5k9Xw5O51cxzlGHMEXvL6ieJYsoZdw4u2LvAEHa0bp7tKZL+5K1ZqMmAsPjO3bAZI8Ak2Hvdx4jy6mkmPo7QKBgQDJOwT4Ztt7TLABcCQ7SmB2Wb0Buvf82YPDYaSkPaV2hVlyC5yd0qF4Z/CsMGETHDYV1Rm8hC0A1taOQNIuFNdzrwedJ38IX5sTDxX9PMrlezDzqZP2RfUG1JqpCXfXPr3Gp06rTnFWJBBeREF3MlF5zi/HOz/N1tma56i2H4is3wKBgBMp8D0/9SKNZaKnULN9QsRCRi02MGvCR0VOPCJXjXx8Y2hoO0zLLq6cVLDoxz0+LqeInPWwLb8SWX/0yRkHMykuKVNc85pBFDs72z+l7XWA9KkkLxHcsVTGVb1IxgPg4zRf5t7fpRz01iMDoIpdkCBFZUOTIqTru6cwAoZIn9CZAoGBAJqQtdGjF+UOU4bXP003ZP6jETWaCk9yN+5EOvVY0JZH+RtLZgInvpVAqTFpe7b1DWoZpEoT8Ilbe7QKgCabJZ6bC6sfokipPvEVvlskbeh0hnYyhA8/TuTvOtiqCB4R+Cz1Z8zXVODIlLYDkZFf2gtIX5SVov2VkNGWLRWvBQlbAoGBAJ6scF1lJ9yu6rPWnfHewgBOxfSJCEVLeBZPSE6KHXXBwGvlz1OhYGsOha6CbVj+I5+FsEPWwA3T/c49EHVnz0wo7NgHKmUwQjcOm4Qr4UEG6Sw23SSSpxnCuJHIyNAOe7PJJnplJNchaQBhrr1MWBqlH597nUDv6BduyepfGnD+";

        var plainText = "Hello World";

        var sign = RSAClass.RSAFromPkcs8.signWithRSASHA256(plainText, privateKey, "UTF-8");
        Console.WriteLine("SDK结果");
        Console.WriteLine(sign);

        // RSASerivce
        byte[] data = Encoding.UTF8.GetBytes(plainText);

        using (var rsa = RSA.Create())
        {

            rsa.ImportFromPem(ToPemFormat(privateKey));
            //Console.WriteLine(keyLength);

            // 使用SHA256哈希算法进行签名
            var result = rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
            Console.WriteLine("我的结果");
            Console.WriteLine(Convert.ToBase64String(result));
        }

    }

    public static void VerifyDemo()
    {
        var publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwCUOWi/RuQfUILpsgTX3Mb67SeGXQQiKGiBhi0kJ3wx11QHOv0lXJ6SpK+o4Df4mg7e3NmK6DcQm2k2UAiBuKUjyD50RPfU91wI2E4jYPEp5TYYjgCgf1NmbgaFrs0bZFPrH3ezHx7sQbWvFKCrBCv+8kCX9kOLSoljkR99BlySHjnnnDi56BecNXPDTnXlgyufayKHD2DQSmgBXgm8q/pUyKG/Ioj56RA4EnT1hShgsBpcglLsf9x+qyAPuqADCvkiEQ2X4MqLKGLBCdSoL//jEe6wjqOj2Iv+feFBf2FYZ/RVdP6IUZ9brERdfBEbICUKK1rl+ImyQB4w7Fm8icwIDAQAB";
        var sign = "K5oCOVZIUqkjkjqJYxiw0xghSF46ak+4oG+aIdJMiJ9rLAFIvAf42UpbAybqEsycf61VSFWrC5hdK5Z/qy4kwGVsKRvvkPzDy1DAWwP1L3++hySSKv+rYGf4ziSJfmcbximmVdinP/YEFhJxIKhLwR3zhpw1blSadxUnLc1k6MU3Ddf3BjMzwsA3FSfyFJJWxbFuQNIyf/XD9jY+areLSUSqX4BJ4IyVsjyT4jqcQG8FPtZEiPju7da5m44+ZGDWdriz5N0j6MIwlCRTWNGUnLdw5nZEtYr1tO3zxLxpnnmGKSr41r/Ihm5h0d2EDRXxeAgdMQR8hWamigTg+KG2qA==";
        var plainText = "Hello World";

        string pemPublicKey = ToPemFormat(publicKey, false);
        // 将签名从 Base64 字符串转换为字节数组
        byte[] signatureBytes = Convert.FromBase64String(sign);

        // 将明文转换为字节数组
        byte[] data = Encoding.UTF8.GetBytes(plainText);

        // 使用 RSA 类导入公钥并验证签名
        using (var rsa = RSA.Create())
        {
            rsa.ImportFromPem(pemPublicKey);

            // 使用 SHA256 哈希算法验证签名
            bool isValid = rsa.VerifyData(data, signatureBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

            Console.WriteLine("验证结果: " + (isValid ? "签名有效" : "签名无效"));
        }
    }
    static string ToPemFormat(string key, bool pri = true)
    {
        // 如果已经是pem格式的key则直接返回
        if (key.StartsWith("-----BEGIN")) return key;

        // 根据pri判断是公钥还是私钥，补充pem格式头尾
        return pri ?
            "-----BEGIN PRIVATE KEY-----" + key + "-----END PRIVATE KEY-----"
            : "-----BEGIN PUBLIC KEY-----" + key + "-----END PUBLIC KEY-----";
    }
}
